package com.smile.lyh.service;

import com.smile.spring.BeanPostProcessor;
import com.smile.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/12/11 17:52
 */
@Component
public class MyBeanPostProcessor  implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        if (beanName.equals("userService")) {
            Object proxyInstance = Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {
                    // 切面
                    System.out.println("切面逻辑");

                    return method.invoke(bean, args);
                }
            });
            return proxyInstance;
        }

        // bean
        return bean;
    }
}
